package com.cn.conf.handler

import com.zhiwangyilian.anno.Uinfo
import org.springframework.core.MethodParameter
import org.springframework.stereotype.Component
import org.springframework.web.bind.support.WebDataBinderFactory
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.context.request.WebRequest
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.method.support.ModelAndViewContainer

@Component
class UserInfoResolver : HandlerMethodArgumentResolver {
    override fun supportsParameter(methodParameter: MethodParameter): Boolean {
        return methodParameter.hasParameterAnnotation(Uinfo::class.java)
    }

    @Throws(Exception::class)
    override fun resolveArgument(
        methodParameter: MethodParameter, modelAndViewContainer: ModelAndViewContainer?,
        nativeWebRequest: NativeWebRequest, webDataBinderFactory: WebDataBinderFactory?
    ): Any? {
        return nativeWebRequest.getAttribute("Current_User", WebRequest.SCOPE_REQUEST) as Uinfo?
    }
}